//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Spam.Rules
{
	/// <summary>
	/// Rates spam factor based on number of links (href's) in the post.
	/// </summary>
	public class LinkSpamRule : SpamRule
	{
		private static readonly Guid ruleID = new Guid("aae386e9-511f-45ba-b5ca-c029ebd2dfe4");
		private static readonly string resourceFile = "ControlPanelResources.xml";
		private static readonly int defaultPoints = 2;
		private static readonly int defaultThreshold = 0;

		public LinkSpamRule()
		{
		}

		public override string Name
		{
			get { return ResourceManager.GetString("spam_LinkRule_Name", resourceFile); }
		}

		public override string Description
		{
			get { return ResourceManager.GetString("spam_LinkRule_Description", resourceFile); }
		}

		public override Guid RuleID
		{
			get { return ruleID; }
		}

		public override ArrayList GetAvailableSettings()
		{
			ArrayList settings = new ArrayList();
			settings.Add(new RuleSetting(ruleID, "points", ResourceManager.GetString("spam_LinkRule_PointsDescription", resourceFile), defaultPoints.ToString()));
			settings.Add(new RuleSetting(ruleID, "threshold", ResourceManager.GetString("spam_LinkRule_ThresholdDescription", resourceFile), defaultThreshold.ToString()));
			return settings;
		}

		public override int RatePost(Post post, CSPostEventArgs e)
		{
			// Verify that this spam rule should process this post
			if (IsExemptFromSpamRating(post, e))
				return 0;

			// Retrieve setting values
			int points = Globals.SafeInt(this.GetSettingValue("points"), defaultPoints);
			int threshold = Globals.SafeInt(this.GetSettingValue("threshold"), defaultThreshold);

			// Count hrefs
			if (!Globals.IsNullorEmpty(post.Body))
			{
				int counter = SpamRuleHelper.CountLinks(post.Body);

				// Score xxx points for each link above the threshold
				if (counter > threshold)
					return (points * (counter - threshold));
			}

			return 0;
		}
	}
}
